package com.jack.hash;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class 有效的数独 {


    public boolean isValidSudoku(char[][] board) {
        int[] hash = new int[9];

        //横排满足条件
        for (int i = 0; i < 9; i++) {
            Arrays.fill(hash, 0);
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    if (hash[board[i][j] - '0' - 1] == 0) {
                        hash[board[i][j] - '0' - 1]++;
                    } else {
                        return false;
                    }
                }
            }
        }
        //竖排满足条件
        for (int i = 0; i < 9; i++) {
            Arrays.fill(hash, 0);
            for (int j = 0; j < 9; j++) {
                if (board[j][i] != '.') {
                    if (hash[board[j][i] - '0' - 1] == 0) {
                        hash[board[j][i] - '0' - 1]++;
                    } else {
                        return false;
                    }
                }
            }
        }
        //3*3满足条件
        for (int i = 0; i < 9; i += 3) {
            for (int j = 0; j < 9; j += 3) {
                Arrays.fill(hash, 0);
                for (int k = i; k < i + 3; k++) {
                    for (int l = j; l < j + 3; l++) {
                        if (board[k][l] != '.') {
                            if (hash[board[k][l] - '0' - 1] == 0) {
                                hash[board[k][l] - '0' - 1]++;
                            } else {
                                return false;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}
